<?php

namespace app\supplier\model\product;

use app\common\model\product\ProductLabel as LabelModel;
use app\common\model\region\Region as Region1;
use app\supplier\model\supplier\Region;

/**
 * 标签模型
 */
class ProductLabel extends LabelModel
{

    /**
     * 获取列表
     * @param $param
     * @param $regionIds
     * @param $SupplierId
     * @return ProductLabel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/28 10:34
     */
    public function getList($param, $SupplierId = 0)
    {
        $model = new static();
        $where = $this->disposeWhere($param);
        $model = $this->assWhere($model, $where);
        $lists = $model->where('shop_supplier_id','=',$SupplierId)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($param);
        
        return $lists;
    }

    public function getSelectList($param, $SupplierId = 0, $fields='*')
    {
        $model = new static();
        $model = $this->where($param);
        $lists = $model->where('shop_supplier_id','=',$SupplierId)
            ->field($fields)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->select();
            
        return $lists;
    }
    
    /**
     * 添加新记录
     */
    public function add($data, $SupplierId)
    {
        $data['app_id'] = self::$app_id;
        $data['shop_supplier_id']    = $SupplierId;
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        $data['audit_status']    = 0;
        unset($data["region_id"]);
        return $this->save($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data, $SupplierId)
    {
        $data['update_time'] = time();
//        $data["audit_status"] = $data['audit_status'] ?? 0;
        $data['audit_status']    = 0;//按照文档需求修改也需要审核
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        if ($SupplierId != $this["shop_supplier_id"]) {
            $this->error = "你无权修改此标签";
            return false;
        }
        return $this->save($data);
    }

    /**
     * 删除
     */
    public function remove()
    {
        // 判断是否存在
        $Count = (new Product())->where('label_id','=' ,$this['id'])
            ->where('product_status','=','10')
            ->where('audit_status','=','20')
            ->where('is_delete','=','0')
            ->count();
        if ($Count > 0) {
            $this->error = '该标签使用中，不允许删除';
            return false;
        }
        return $this->delete();
    }

    public static function assWhere($model, $where)
    {
        foreach ($where as $k => $item) {
            if ($item == '') {
                unset($where[$k]);
            }
        }
        if (array_key_exists('search', $where) && $where['search'] != '') {
            $model = $model->where('label_name', 'LIKE', '%' . $where['search'] . '%');
            unset($where['search']);
        } else {
            unset($where['search']);
        }
        if (array_key_exists('create_time', $where) && $where['create_time'][0] != '') {
            $model = $model->where('create_time', '>=', strtotime($where['create_time'][0]));
            $model = $model->where('create_time', '<=', strtotime($where['create_time'][1]));
            unset($where['create_time']);
        } else {
            unset($where['create_time']);
        }

        return $model->where($where);
    }
 
}